package com.antcloud.demo.antscheduler.handlers;

import java.util.Random;
import java.util.concurrent.ThreadPoolExecutor;

import com.alipay.antschedulerclient.common.ClientCommonResult;
import com.alipay.antschedulerclient.handler.ISimpleJobHandler;
import com.alipay.antschedulerclient.model.JobExecuteContext;
import org.apache.commons.lang.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChildOneHandler implements ISimpleJobHandler {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Override
    public String getName() {
        return "childOneHandler";
    }

    @Override
    public ClientCommonResult handle(JobExecuteContext context) {
        LOGGER.info("[CallbackHandler] handle callback job");
        Boolean isAlwaysSuccess = (Boolean) context.getCustomParam("alwaysSuccess");
        if (isAlwaysSuccess) {
            return ClientCommonResult.buildSuccessResult();
        }
        Random random = new Random();
        int sleepTime = random.nextInt(5000);
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            return ClientCommonResult.buildFailResult("sleep error");
        }
        if (SuccessUtil.isSucess(50)) {
            return ClientCommonResult.buildSuccessResult();
        } else {
            return ClientCommonResult.buildFailResult("handle failed");
        }
    }

    @Override
    public ThreadPoolExecutor getThreadPool() {
        return null;
    }

}
